package com.probiz.estoresa.product.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.product.StockNotify;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.product.model.StockNotifySearchCriteria;
import com.probiz.estore.product.service.StockNotifyManager;

public class StockNotifyController extends GenericStoreAdminController<StockNotify> {
    private StockNotifyManager stockNotifyManager = null;

    public void setStockNotifyManager(StockNotifyManager inMgr) {
        this.stockNotifyManager = inMgr;
    }

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(StockNotify entity) {
		return entity.getStockNotifyName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = stockNotifyManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#onSave(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void preSave(HttpServletRequest request, StockNotify entity, BindException errors) {
	}
	
	public ModelAndView defaultAction(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("product/stockNotifyList");
		return mv;
	}
	
	public ModelAndView search(HttpServletRequest request,	HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("product/include/stockNotifyListBody");
		SearchCriteria searchCriteria = createSearchCriteria(request);
		StockNotifySearchCriteria stockNotifySearchCriteria = new StockNotifySearchCriteria();
		try {
			// 绑定参数
			bind(request, stockNotifySearchCriteria);
			List<StockNotify> stockNotifyList = stockNotifyManager.searchByCriteriaForController(stockNotifySearchCriteria,searchCriteria);
			mv.addObject("stockNotifyList", stockNotifyList);
			mv.addObject("stockNotifySearchCriteria", stockNotifySearchCriteria);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return mv;
	}
	
	
	public ModelAndView multiSendNotify(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			for(int i=0; i< ids.length; i++){
				StockNotify stockNotify = stockNotifyManager.getById(Integer.parseInt(ids[i]));
				try{
					stockNotifyManager.sendStockNotify(stockNotify);
				}catch(Exception e)	{
					e.printStackTrace();
				}
			}
			String message = getMessage("stockNotify.send.success", new Object[] {ids.length });
			data.put("msg", message);
		}
		return this.getModelAndAjaxJsonView(data);
	}
	
	public ModelAndView multiDelete(HttpServletRequest request,	HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		String[] ids = request.getParameterValues("multiIds");
		if (ids != null && ids.length > 0) {
			try{
				stockNotifyManager.deleteAllByIds(ids);
			}catch (DataIntegrityViolationException e) {
			}
			String message = getMessage("common.deleted.multi", new Object[] {
					getEntityTypeMessage(), ids.length });
			data.put("msg", message);
		}
		return this.getModelAndAjaxJsonView(data);
	}

	

}
